import com.webobjects.appserver.*;
import com.webobjects.foundation.*;
import com.webobjects.eocontrol.*;
import groovy.lang.webobjects.*;
import groovy.lang.webobjects.categories.*;
import groovy.lang.webobjects.components.*;

public class Main extends WOComponent {

	private WOGroovyShell shell = WOGroovyShell.shellWithDefaultBinding();
	public WOTranscript transcript = WOTranscript.defaultTranscript();
	public String command;
	public String test1 = "Testing ";
	
    public Main(WOContext context) {
        super(context);
        shell.setObjectForKey(this, "self");
    }
    
    public String mapsAndLists() {
    	// Groovy lets you use NSArrays and NSDictionaries like regular lists and maps
    	// (you still need to use the mutable equivalent to edit them inplace)
    	
    	//Lets start with a hashmap
    	 def lang = [best: 'groovy', better: 'objective-c', good: 'java' ];
    	
	 	// Lets make a new NSMutableDictionary from the hashmap
 	 	NSMutableDictionary nslang = lang as  NSMutableDictionary;
	 	
	 	//We can use subscript syntax to interact with NSDictionaries
	 	assert nslang instanceof NSMutableDictionary
    	nslang['better'] = 'smalltalk';
	 	
	 	//Put the languages into their own array and capitalize first letter using a closure
	 	NSArray langList = nslang.collect { it -> 
	 		it.value.toString()[0].toUpperCase() + it.value.toString()[1..-1]; 
	 	} 
	 		 	
 		//Lets make it an NSMutableArray
	 	NSMutableArray nslangList = langList  as NSArray;
 
	 	//Add some new elements to the array
	 	nslangList << 'perl' << 'python' << 'c' << 'ada' << 'prolog';

	 	// We use a category to add a random() function to the Integer class
	 	use (IntegerAdditions) {
		 	//Change a random element
	 		nslangList[nslangList.size().random()] = 'basic';
	 	}
	 	 	
	 	// Build an array of 'Language' GroovyBeans using a closure and Groovy map syntax
	 	NSMutableArray beanList = nslangList.collect { new Language([ language: it, length: it.size() ]) }
	 	
	 	// Lets build an EOQualifier to search for names with 4 characters or more
	 	EOQualifier qual = 'length >= 4'.qualifier();

	 	// Lets find matching array elements
	 	beanList = beanList.filteredArrayWithQualifier(qual);
	 	
	 	// Lets sort the language names into an NSArray in descending order of length
	 	NSArray longNames = beanList.sort('length').reverse().language;
	 	
	 	return longNames.join(' : ');
    }
    
    public Number random() {
    	Integer i;
    	use (IntegerAdditions) {
    		i = 10000.random() - 5000;
    	}
    	return i;
    }
    
    public String example1() {
    	return 'This is a String'.valueForKey('length');
    }
    
    public String example2() {
    	return 1173120000.unixDate();
    }
    
    public NSNumberFormatter example3() {
    	return "#,##0.00;-(#,##0.00)".numberFormatterForPattern();
    }
    
    public String example4() {
    	return "some.property.key.path".lastPropertyKey();
    }

    public String example5() {
    	return "some.property.key.path".displayName();
    }

    public String example6() {
    	return "tomorrow".timestamp();
    }
    
    public String example7() {
    	return "yes".booleanValue();
    }
    
    public String shell() {
    	return WOTranscript.defaultTranscript();	
    }
    
    public WOComponent doCommandAction() {
    	if (command) {
    		try {
        		show(">" + command + "\n");
    			show(shell.evaluate(command));
    			show("\n");
    		} catch (Exception e) {
    			show(e.toString() + "\n");
    		}
    	}
    	command = '';
    	return null;
    }
    
    public WOComponent clearTranscriptAction() {
    	transcript.reset();
    }
    
    public WOComponent doSomething() {
    	test1 = "doSomething";
    	return context().page();
    }

    public String someString() {
    	return "someString";
    }
    
}

//Lets create a category for Integer to add a random function
public class IntegerAdditions {
	public static Integer random(Integer self) {
		return Math.random() * self;
	}
}

